<template>
  <div class="list" ref='wrapper'>
    <div>
      <div class="area">
        <div class="title">当前城市</div>
        <div class="button-wrap">
          <div class="button" @click='handleChangeCity(city)'>{{this.city}}</div>
        </div>
      </div>
      <div class="area">
        <div class="title">热门城市</div>
        <div class="button-wrap" >
          <div class="button" v-for="item in hotCities" :key="item.id" @click='handleChangeCity(item.name)'>{{item.name}}</div>
        </div>
        </div>
      <div class="area" v-for="(item,key) in cities" :key="key" :ref='key'>
        <div class="title" >{{key}}</div>
        <div class="item-list">
          <div class="item" v-for="innerItem in item" :key="innerItem.id" @click='handleChangeCity(innerItem.name)'>{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import BScroll from 'better-scroll'
export default {
  name: 'list',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    ...mapMutations(['changeCity']),
    handleChangeCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
  },
  updated () {
    this.scroll = new BScroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
   @import '~styles/varibles.styl'
   .list
    position: absolute;
    overflow: hidden;
    top: 1.7rem;
    left: 0;
    right: 0;
    bottom: 0;
    .title
      line-height: .4rem;
      padding: .1rem;
      background-color: #f1f1f1;
      color: #333;
    .button-wrap
      overflow:hidden
    .button
      float: left;
      line-height: .4rem;
      width: 2rem;
      margin: .1rem;
      border: .02rem solid #ccc
    .item-list
      .item
        line-height: .6rem;
        border-bottom:.02rem solid #ccc
        padding-left: .1rem;
</style>
